﻿//#define debug
#define GX
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;

namespace BusinessDll
{
    public class BusinessControl
    {
        public static CommandList commandList = GlobalInfo.Instance.commandList;
        public static String sessionID;
        public static String pathImg;
        public static String GrossWeight;
        public static String OLD_VE_LICENSE_NO;

        public static int LogNum = 0;

        public DBWeight dbWeight;

        SocketServer serv = null;
        private static Dictionary<Command, AbsCommandLogic> logic;
        public Thread commandThread ;
        public Thread rdThread;
        public static int readRFID = 0;
        public static int RFIDSLEEP = 30;


        public BusinessControl() {
            logic = new Dictionary<Command, AbsCommandLogic>();
            //AbsCommandLogic absCommandLogic = new CarLogic();
            logic.Add(Command.car, new CarLogic());
            logic.Add(Command.operation,new OperationLogic());
            logic.Add(Command.rfcar,new RfCarLogic());
            //vECtrl = new HKCtrl();
        }
        public void init() {

            Console.WriteLine("PLC: " + GlobalInfo.Instance.generalParam["PLC"]);
            showOnUI("程序", "-----------------------程序开始启动------------------------");
            showOnUI("设备准备", "开始准备设备");


            taskInit(0);

            serv.StartListen();
            commandThread = new Thread(getCommnd);
            commandThread.Name = "命令运行";
            commandThread.Priority = ThreadPriority.AboveNormal;
            commandThread.Start();


        }
        public static void getCommnd() {
            LineData lineData = new LineData();
            WriteLog.WriteLogs("start","thread start");
            while (CommandList.semaphoreLink.WaitOne())
            {
                try
                {

                    //
                    lineData = commandList.popCommandLine();
                    WriteLog.WriteLogs("thread", "command is" + lineData.command);
                    runLogic(lineData);
                }
                catch (Exception ex)
                {
                    WriteLog.printException(ex);
                    
                }
                Thread.Sleep(100);
            }
        }
        public static void runLogic(LineData _lineData) {
            if (logic.ContainsKey(_lineData.command)) {
                logic[_lineData.command].param = _lineData.body;
                logic[_lineData.command].Run();
            }
        }
        public static void showOnLed(string content)
        {
#if debug
            try
            {
                EQ_LEDCtrl ledCtrl = new EQ_LEDCtrl();
                ledCtrl.realtimeclear();
                if (ledCtrl.LEDConnect())
                {
                    ledCtrl.LEDSender(content);
                    ledCtrl.tfi_close();
                }
            }
            catch (Exception ex)
            {
                WriteLog.printException(ex);
            }
#else

#endif
        }
        public void showOnUI(string module, string info)
        {
            //comment by zt: 日志显示到程序控制台
            //gateMain.showOnUI(module, info);

            LogNum++;
            WriteLog.WriteLogs(module,info);
        }
        public void taskInit(int status)
        {

            //showOnLed("欢迎光临");
            if (status != 3)
            {

                serv.StartListen();

            }

        }

        public void exit() {
            
            commandThread.Abort();
            serv.stop();
            WriteLog.WriteLogs("退出", "退出流程完成==============");


        }
    }
}
